package com.jk.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jk.dao.OrderDao;
import com.jk.dao.SecKillDao;
import com.jk.entity.OrderEntity;
import com.jk.entity.SecondKillEntity;
import com.jk.service.SecKillService;
import com.jk.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SecKillServiceImpl implements SecKillService {

    @Autowired
    private SecKillDao secKillDao;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public List<SecondKillEntity> findSec(SecondKillEntity secondKillEntity) {
        //日期转换

        String time =sdf.format(new Date());
        List<SecondKillEntity> list = secKillDao.selectSecList(secondKillEntity);
        list.forEach(obj->{
            String startDate = obj.getStartDate();
//            long[] toStart = DateUtil.getDistanceTimes(time, startDate);
//            long[] toEnd = DateUtil.getDistanceTimes(time, obj.getEndDate());
//            obj.setToStart(toStart);
//            obj.setToEnd(toEnd);
        });

        return list;
    }

    @Override
    public List<SecondKillEntity> findSecfour() {
        return secKillDao.findSecfour();
    }

    @Override
    public int findCount(Integer id) {

        QueryWrapper<SecondKillEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("proId",id);
        SecondKillEntity secondKillEntity = secKillDao.selectOne(wrapper);
        return secondKillEntity.getPurCount();
    }

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void saveOrder(OrderEntity product) {
        product.setOrderType(2);
        mongoTemplate.save(product);
    }

    @Override
    public void addKill(SecondKillEntity secondKill) {
        secondKill.setStatus(1);
        secKillDao.insert(secondKill);
    }

    @Override
    public void buy(Integer proId) {
        secKillDao.jianCount(proId);
    }

    @Override
    public List<SecondKillEntity> findSecAll(SecondKillEntity secondKill) {

        QueryWrapper<SecondKillEntity> wrapper = new QueryWrapper<>();
        if (secondKill.getProname()!=null && secondKill.getProname()!=""){
            wrapper.like("proname",secondKill.getProname());
        }
        List<SecondKillEntity> list = secKillDao.selectList(wrapper);
        return list;
    }

    @Override
    public void delSecPro(Integer id) {
        secKillDao.deleteById(id);
    }
}
